package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult selectAllSetmael(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<Setmeal> list = setmealMapper.selectAllSetmeal(setmealPageQueryDTO);
        Page<Setmeal> page = (Page<Setmeal>) list;
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void insertSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateTime(LocalDateTime.now());

        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insertSetmeal(setmeal);

        Long id = setmeal.getId();
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : list) {
            setmealDish.setSetmealId(id);
        }
        setmealDishMapper.insertSetmealDish(list);
    }

    @Override
    @Transactional
    public void deletesSetmine(List<Integer> ids) {
        //判断该套餐有没有售卖
        for (Integer id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            if(setmeal.getStatus() == 1){
                throw new DeletionNotAllowedException("该套餐正在售卖中，不能删除");
            }
        }
        //删除套餐
        setmealMapper.deletesSetmine(ids);
        //删除套餐和菜品的关联关系数据
        setmealDishMapper.deletesSetmealDish(ids);
    }

    @Override
    public SetmealDTO getById(Integer id) {
        //查询出套餐数据
        Setmeal setmeal = setmealMapper.selectById(id);
        //查询出套餐关联菜品数据
        List<SetmealDish> list = setmealDishMapper.getById(id);
        SetmealDTO setmealDTO = new SetmealDTO();
        setmealDTO.setSetmealDishes(list);
        BeanUtils.copyProperties(setmeal,setmealDTO);
        return setmealDTO;
    }

    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        setmeal.setUpdateTime(LocalDateTime.now());
        Long id = BaseContext.getCurrentId();
        setmeal.setUpdateUser(id);
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);
        setmealDishMapper.deletes(setmealDTO.getId());
        Long idd = setmeal.getId();
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : list) {
            setmealDish.setSetmealId(idd);
        }
        setmealDishMapper.insertSetmealDish(list);
    }

    @Override
    public void updateSetmeals(Integer status, Long id) {
        //起售时判断菜品是否有停售
        if(status == StatusConstant.ENABLE){
            List<Dish> list = dishMapper.selectBySetmealId(id);
            for (Dish dish : list) {
                if(dish.getStatus() == StatusConstant.DISABLE){
                    throw new DeletionNotAllowedException("菜单中有菜品已被停售，套餐不能起售");
                }
            }
        }
        setmealMapper.updateSetmeals(status,id);
    }
}
